package com.example.demo.mapper;

import com.example.demo.entity.StudentProgress;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentProgressMapper {

    // 根据学生ID查找进度
    @Select("SELECT * FROM student_course_progress WHERE student_id = #{studentId}")
    List<StudentProgress> findByStudentId(@Param("studentId") Long studentId);

    // 根据学生ID和课程ID查找进度
    @Select("SELECT * FROM student_course_progress WHERE student_id = #{studentId} AND course_id = #{courseId}")
    List<StudentProgress> findByStudentIdAndCourseId(@Param("studentId") Long studentId, @Param("courseId") Long courseId);

    // 根据状态查找进度
    @Select("SELECT * FROM student_course_progress WHERE student_id = #{studentId} AND completion_status = #{completionStatus}")
    List<StudentProgress> findByStudentIdAndCompletionStatus(@Param("studentId") Long studentId, @Param("completionStatus") String completionStatus);

    // 插入学习进度
    @Insert("INSERT INTO student_course_progress (student_id, course_id, assignment_id, completion_status, score, updated_at) " +
            "VALUES (#{studentId}, #{courseId}, #{assignmentId}, #{completionStatus}, #{score}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(StudentProgress progress);

    // 更新学习进度
    @Update("UPDATE student_course_progress SET completion_status = #{completionStatus}, score = #{score}, updated_at = #{updatedAt} " +
            "WHERE id = #{id}")
    int update(StudentProgress progress);

    // 根据ID删除进度
    @Delete("DELETE FROM student_course_progress WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    // 计算学生总分
    @Select("SELECT COALESCE(SUM(score), 0) FROM student_course_progress WHERE student_id = #{studentId} AND score IS NOT NULL")
    Double calculateStudentTotalScore(@Param("studentId") Long studentId);

    // 统计完成的任务数
    @Select("SELECT COUNT(*) FROM student_course_progress WHERE student_id = #{studentId} AND completion_status = 'COMPLETED'")
    Integer countCompletedTasks(@Param("studentId") Long studentId);
}